package cn.xjt.screenShare.netty;

import cn.xjt.screenShare.constant.EnumConstant;
import cn.xjt.screenShare.netty.decoder.ImageEncoder;
import cn.xjt.screenShare.netty.share.CaptureImage;
import cn.xjt.screenShare.util.ImageUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NettyServer{
    public static void main(String[] args) {
        getInstance().start();
    }

    private NettyServer(){

    }

    private ExecutorService executorServices =  Executors.newCachedThreadPool();

    private static NettyServer nettyServer;

    public CaptureImage captureImage = new CaptureImage();
    public StringDecoder stringDecoder = new StringDecoder(StandardCharsets.UTF_8);

    public static NettyServer getInstance(){
        if (nettyServer==null){
            nettyServer = new NettyServer();
        }
        return nettyServer;
    }
    NioEventLoopGroup boss;
    NioEventLoopGroup work;
    public ChannelFuture future;
    private ImageEncoder imageEncoder = new ImageEncoder();

    private ChannelInitializer channelInitializer = new ChannelInitializer<NioSocketChannel>() {
        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            executorServices.submit(()->{
                while (true){
                    byte[] bytes = ImageUtil.catScreen();
                    captureImage.setContent(bytes);
                    captureImage.setLength(bytes.length);
                    ch.writeAndFlush(captureImage);
                }
            });
        }
    };
    private ChannelInitializer handle = new ChannelInitializer<NioSocketChannel>() {
        @Override
        protected void initChannel(NioSocketChannel channel) {
            ChannelPipeline pipeline = channel.pipeline();
            pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
            pipeline.addLast(imageEncoder);
            pipeline.addLast(channelInitializer);
        }

    };
    public void start(){
        boss = new NioEventLoopGroup();
        work = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap()
                .group(boss,work)
                .channel(NioServerSocketChannel.class);
        future = bootstrap.childHandler(handle)
                .bind(EnumConstant.Port.SERVER.port);
        try {
            future.channel().closeFuture().sync();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            boss.shutdownGracefully();
            work.shutdownGracefully();
        }
    }

    public void stop(){
        boss.shutdownGracefully();
        work.shutdownGracefully();
    }

    public static void run(){
        //            服务器共享
        NettyServer.getInstance().start();
    }

}
